package com.personal.o2o.util;

import javax.servlet.http.HttpServletRequest;

public class HttpServletRequestUtil {

    /**
     * 从request中提取一个key,将他的值转换为整型
     * @param request
     * @param key
     * @return
     */
    public static int getInt(HttpServletRequest request, String key){
        try{
            return Integer.decode(request.getParameter(key));
        }catch (Exception e){
            return -1;
        }
    }

    /**
     * 从request中提取一个key,将他的值转换为长整型
     * @param request
     * @param key
     * @return
     */
    public static long getLong(HttpServletRequest request, String key){
        try{
            return Long.valueOf(request.getParameter(key));
        }catch (Exception e){
            return -1;
        }
    }


    /**
     * 从request中提取一个key,将他的值转换为双精度
     * @param request
     * @param key
     * @return
     */
    public static Double getDouble(HttpServletRequest request, String key){
        try{
            return Double.valueOf(request.getParameter(key));
        }catch (Exception e){
            return -1d;
        }
    }

    /**
     * 从request中提取一个key,将他的值转换为布尔
     * @param request
     * @param key
     * @return
     */
    public static boolean getBoolean(HttpServletRequest request, String key){
        try{
            return Boolean.valueOf(request.getParameter(key));
        }catch (Exception e){
            return false;
        }
    }

    /**
     * 从request中提取一个key,将他的值做简单处理
     * @param request
     * @param key
     * @return
     */
    public static String getString(HttpServletRequest request, String key){
        try{
            String result = request.getParameter(key);
            if (result!=null) {
                result = result.trim();
            }
            if ("".equals(result)) {
                return null;
            }
            return result;
        }catch (Exception e){
            return null;
        }
    }

}
